﻿Imports System
Imports System.IO
Imports System.Text

''' <summary>
''' LogFile object representing a text log file.
''' </summary>
Public Class LogFile

#Region "Constants"

	Private Const SEPARATOR As String = "------------------------------------------------------------"

#End Region

#Region "Variables"

	Private LogBuilder As StringBuilder
	Private StartedAt As DateTime

	Private _FilePath As String
	Private _Title As String
	Private _Timestamp As Boolean

#End Region

#Region "Properties"

	''' <value>
	''' The contents of the log.
	''' </value>
	Public ReadOnly Property Contents() As String
		Get
			Return LogBuilder.ToString()
		End Get
	End Property

	''' <value>
	''' The full path for the log file.
	''' </value>
	Public Property FilePath() As String
		Get
			Return _FilePath
		End Get
		Set(ByVal value As String)
			_FilePath = value
		End Set
	End Property

	''' <value>
	''' The log descriptive title.
	''' </value>
	Public Property Title() As String
		Get
			Return _Title
		End Get
		Set(ByVal value As String)
			_Title = value
		End Set
	End Property

	''' <value>
	''' If true, will put a timestamp for every action.
	''' </value>
	Public Property Timestamp() As Boolean
		Get
			Return _Timestamp
		End Get
		Set(ByVal value As Boolean)
			_Timestamp = value
		End Set
	End Property

#End Region

#Region "Default methods"

	''' <summary>
	''' Creates a new LogFile object.
	''' </summary>
	Public Sub New()

		Init(String.Empty, String.Empty)

	End Sub

	''' <summary>
	''' Creates a new LogFile object and sets the full file path.
	''' </summary>
	Public Sub New(ByVal path As String)

		Init(path, String.Empty)

	End Sub

	''' <summary>
	''' Creates a new LogFile object with the specified file path and title.
	''' </summary>
	Public Sub New(ByVal path As String, ByVal title As String)

		Init(path, title)

	End Sub

	''' <returns>Returns the contents of the LogFile.</returns>
	Public Overloads Overrides Function ToString() As String

		Return Contents

	End Function

#End Region

#Region "Logging"

	''' <summary>
	''' Clears the log contents and set the start date as Now().
	''' </summary>
	Public Sub Clear()

		StartedAt = DateTime.Now

		LogBuilder = New StringBuilder()

	End Sub

	''' <summary>
	''' Initialize the LogFile using the specified file path and log title.
	''' </summary>
	Public Sub Init(ByVal newPath As String, ByVal newTitle As String)

		Clear()

		FilePath = newPath
		Title = newTitle

	End Sub

	''' <summary>
	''' Appends a line separator to the log file.
	''' </summary>
	Public Sub AppendSeparator()

		Append(SEPARATOR)

	End Sub

	''' <summary>
	''' Appends a new line to the log file.
	''' </summary>
	Public Sub Append()

		Append(String.Empty)

	End Sub

	''' <summary>
	''' Appends the text in a new line in the log file.
	''' </summary>
	Public Sub Append(ByVal text As String)

		Append(text, Timestamp)

	End Sub

	''' <summary>
	''' Appends the text in a new line, and adds a timestamp if true.
	''' </summary>
	Public Sub Append(ByVal text As String, ByVal appendTimestamp As Boolean)

		If Not String.IsNullOrEmpty(text) And appendTimestamp Then
			LogBuilder.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
			LogBuilder.Append(": ")
		End If

		LogBuilder.Append(text)
		LogBuilder.Append(Environment.NewLine)

	End Sub

	''' <summary>
	''' Appends the specified exception to the log file.
	''' </summary>
	Public Sub AppendError(ByVal ex As Exception)

		Append("ERROR: " & ex.Message)

	End Sub

	''' <summary>
	''' Saves the LogFile to its file path.
	''' </summary>
	Public Sub Save()

		Save(True)

	End Sub

	''' <summary>
	''' Saves the LogFile to its file path, and appends the date after the filename.
	''' </summary>
	Public Sub Save(ByVal appendDate As Boolean)

		Dim header As String = String.Empty
		Dim footer As String = String.Empty
		Dim text As String = Contents
		Dim filename As String = Path.GetFileNameWithoutExtension(FilePath)

		If appendDate Then
			filename &= "." & DateTime.Now.ToString("yyyyMMdd-HHmmss")
		End If

		filename = Path.GetDirectoryName(FilePath) & "\" & filename & Path.GetExtension(FilePath)

		If Not Timestamp Then

			header &= "START: " & DateTime.Now.ToShortDateString() & " " & DateTime.Now.ToLongTimeString()
			header &= Environment.NewLine
			header &= SEPARATOR
			header &= Environment.NewLine

			footer &= SEPARATOR
			footer &= Environment.NewLine
			footer &= "END: " & DateTime.Now.ToShortDateString() & " " & DateTime.Now.ToLongTimeString()
			footer &= Environment.NewLine
			footer &= SEPARATOR
			footer &= Environment.NewLine

			text = header & text & footer

		End If

		IOUtil.WriteFile(filename, text, True)

	End Sub

#End Region

End Class